package org.gruposp2p.aula.gwt.client.content;

import org.gruposp2p.aula.gwt.client.AulaApplication;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

public class AulaAuthenticateDialogBox extends DialogBox {
	
	private AulaApplication aulaApplication;
	
	public static interface CwConstants extends Constants {
		String AulaAuthenticateDialogBoxCaption();
		String AulaAuthenticateDialogBoxRegisterCaption();
		String AulaAuthenticateDialogCancel();
		String AulaAuthenticateDialogAccept();
		String AulaAuthenticateDialogRegisterText();
		String AulaAuthenticateDialogNameLabel();
		String AulaAuthenticateDialogSurnameLabel();
		String AulaAuthenticateDialogEmailLabel();
		String AulaAuthenticateDialogRegisteredText();
		String AulaAuthenticateDialogRegister();
		String AulaAuthenticateDialogPassword();
		String AulaAuthenticateDialogRepeatPassword();

	}
	  /**
	   * An instance of the constants.
	   */
	  private CwConstants constants;
	  //private FlexTable layout;
	
	public AulaAuthenticateDialogBox (AulaApplication aulaApplication) {
		this.aulaApplication = aulaApplication;
		constants = aulaApplication.getConstants();
		//layout = new FlexTable();
	    // Create a dialog box and set the caption text
        ensureDebugId("AulaAuthenticateDialogBox");
        showEnterDialog();
	}
	
	private void showEnterDialog () {
		setText(constants.AulaAuthenticateDialogBoxCaption());  
		FlexTable layout = new FlexTable(); 
		layout.clear();
	    layout.setCellSpacing(6);
	    layout.setWidth("250px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    layout.setHTML(0, 0, constants.AulaAuthenticateDialogEmailLabel());
	    TextBox emailTextBox = new TextBox ();
	    layout.setWidget(0, 1, emailTextBox);
	    layout.setHTML(1, 0, "Contraseña:");
	    layout.setWidget(1, 1, new TextBox());
	    
	    HTML text0 = new HTML(constants.AulaAuthenticateDialogRegisterText());

	    text0.addClickHandler( new ClickHandler() {
	          public void onClick(ClickEvent event) {
	        	  showRegisterDialog();
	          }
	        });
	    layout.setWidget(2, 0, text0);
	    cellFormatter.setColSpan(2, 0, 2);
	    cellFormatter.setHorizontalAlignment(2, 0,
	        HasHorizontalAlignment.ALIGN_LEFT);

	    // Add a close button at the bottom of the dialog
	    Button cancelButton = new Button(constants.AulaAuthenticateDialogCancel(),
	        new ClickHandler() {
	          public void onClick(ClickEvent event) {
	            hide();
	          }
	        });
	    Button acceptButton = new Button(constants.AulaAuthenticateDialogAccept(),
		        new ClickHandler() {
		          public void onClick(ClickEvent event) {
		            hide();
		          }
		        });
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);
	    hPanel.add(cancelButton);
	    hPanel.add(acceptButton);

	    layout.setWidget(3, 1, hPanel);
	    cellFormatter.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	    setWidget(layout);
	    //To solve Firefox strange effect
	    emailTextBox.setFocus(true);
	}
	
	private void showRegisterDialog () {
		
		setText(constants.AulaAuthenticateDialogBoxRegisterCaption());      
		FlexTable layout = new FlexTable(); 
	    layout.setCellSpacing(6);
	    layout.setWidth("250px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    layout.setHTML(0, 0, constants.AulaAuthenticateDialogNameLabel());
	    layout.setWidget(0, 1, new TextBox());
	    layout.setHTML(1, 0,  constants.AulaAuthenticateDialogSurnameLabel());
	    layout.setWidget(1, 1, new TextBox());
	    layout.setHTML(2, 0,  constants.AulaAuthenticateDialogEmailLabel());
	    TextBox emailTextBox = new TextBox ();
	    layout.setWidget(2, 1, emailTextBox);	    
	    layout.setHTML(3, 0,  constants.AulaAuthenticateDialogPassword());
	    layout.setWidget(3, 1, new TextBox());	 
	    layout.setHTML(4, 0,  constants.AulaAuthenticateDialogRepeatPassword());
	    layout.setWidget(4, 1, new TextBox());	 
   

	    // Add a close button at the bottom of the dialog
	    Button cancelButton = new Button(constants.AulaAuthenticateDialogCancel(),
	        new ClickHandler() {
	          public void onClick(ClickEvent event) {
	        	  showEnterDialog();
	          }
	        });
	    Button acceptButton = new Button(constants.AulaAuthenticateDialogAccept(),
		        new ClickHandler() {
		          public void onClick(ClickEvent event) {
		        	  showRegisteredialog();
		          }
		        });
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);
	    hPanel.add(cancelButton);
	    hPanel.add(acceptButton);
	    layout.setWidget(5, 1, hPanel);
	    cellFormatter.setHorizontalAlignment(5, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	    setWidget(layout);
	    //To solve Firefox strange effect
	    emailTextBox.setFocus(true);
	}
	
	private void showRegisteredialog () {
		setText(constants.AulaAuthenticateDialogBoxRegisterCaption());  
		FlexTable layout = new FlexTable(); 
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
		layout.clear();
	    layout.setCellSpacing(6);
	    layout.setWidth("250px");	
	    HTML textRegistered = new HTML(constants.AulaAuthenticateDialogRegisteredText());
	    layout.setWidget(0, 0, textRegistered);
	    cellFormatter.setColSpan(0, 0, 2);
	    cellFormatter.setHorizontalAlignment(2, 0,
	        HasHorizontalAlignment.ALIGN_LEFT);	
	    
	    Button acceptButton = new Button(constants.AulaAuthenticateDialogAccept(),
		        new ClickHandler() {
		          public void onClick(ClickEvent event) {
		        	  showEnterDialog();
		          }
		        });
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);
	    hPanel.add(acceptButton);
	    layout.setWidget(1, 1, hPanel);
	    cellFormatter.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	    setWidget(layout);
	}

}
